<template>
  <view class="normal-login-container">
    <view class="logo-content">
      <text class="title">考核系统登录</text>
    </view>
    <view class="login-form-content">
      <view class="input-item flex align-center">
        <view class="iconfont icon-user icon"></view>
        <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30"
          confirm-type="next" :adjust-position="true" @focus="handleFocus" @blur="handleBlur" />
      </view>
      <view class="input-item flex align-center">
        <view class="iconfont icon-password icon"></view>
        <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20"
          confirm-type="done" :adjust-position="true" @focus="handleFocus" @blur="handleBlur" />
      </view>
      <view class="action-btn">
        <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg">登录</button>
      </view>
    </view>

  </view>
</template>

<script>
import { getCodeImg } from '@/api/login'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: "",
      captchaEnabled: true,
      // 用户注册开关
      register: false,
      globalConfig: getApp().globalData.config,
      loginForm: {
        username: "admin",
        password: "admin123",
        code: "",
        uuid: ""
      }
    }
  },
  created() {
    this.getCode()
  },
  methods: {

    handleFocus() {
      // 输入框获得焦点时的处理
      console.log('输入框获得焦点')
    },
    handleBlur() {
      // 输入框失去焦点时的处理
      console.log('输入框失去焦点')
    },



    // 用户注册
    handleUserRegister() {
      this.$tab.redirectTo(`/pages/register`)
    },
    // 隐私协议
    handlePrivacy() {
      let site = this.globalConfig.appInfo.agreements[0]
      this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    },
    // 用户协议
    handleUserAgrement() {
      let site = this.globalConfig.appInfo.agreements[1]
      this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    },
    // 获取图形验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    // 登录方法
    async handleLogin() {
      if (this.loginForm.username === "") {
        this.$modal.msgError("请输入账号")
      } else if (this.loginForm.password === "") {
        this.$modal.msgError("请输入密码")
      } else {
        this.$modal.loading("登录中，请耐心等待...")
        this.pwdLogin()
      }
    },
    // 密码登录
    async pwdLogin() {
      this.$store.dispatch('Login2', this.loginForm).then(() => {
        this.$modal.closeLoading()
        this.loginSuccess()
      }).catch(() => {
        if (this.captchaEnabled) {
          this.getCode()
        }
      })
    },
    // 登录成功后，处理函数
    loginSuccess(result) {
      // 设置用户信息
      this.$store.dispatch('GetInfo').then(res => {
        // console.log(res);

        // this.$tab.reLaunch('/pages/assessList/index')
        // 通知父组件将LoginShow这设置为false
        this.$emit('loginSuccess', true)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.normal-login-container {
  width: calc(100vw * 1864 / 2560);
  height: calc(100vh * 940 / 1440);
  // background-color: transparent;
  background: url('@/static/images/index/bg_login@2x.png') no-repeat center center;
  background-size: cover;

  .logo-content {
    width: 100%;
    height: calc(100vh * 100 / 1440) !important;
    font-size: calc(100vw * 48 / 2560);
    text-align: center;
    padding-top: calc(100vw * 191 / 2560);

    image {
      border-radius: calc(100vw * 4 / 2560);
    }

    .title {
      margin-left: calc(100vw * 10 / 2560);
    }
  }

  .login-form-content {
    text-align: center;
    margin: calc(100vw * 20 / 2560) auto;
    margin-top: calc(100vw * 160 / 2560);
    width: 80%;

    .input-item {
      width: calc(100vw * 600 / 2560);
      margin: calc(100vw * 20 / 2560) auto;
      background-color: transparent;
      height: calc(100vw * 90 / 2560);
      border: 2px solid #2580EC;
      border-radius: calc(100vw * 20 / 2560);

      .icon {
        font-size: calc(100vw * 38 / 2560);
        margin-left: calc(100vw * 20 / 2560);
        color: #999;
      }

      .input {
        width: 100%;
        width: 100% !important;
        font-size: calc(100vw * 40 / 2560);
        line-height: calc(100vw * 20 / 2560);
        text-align: left;
        padding-left: calc(100vw * 15 / 2560);
      }

      .uni-input-placeholder {
        width: 100%;
        height: 100% !important;
        line-height: 100% !important;
        top: calc(100vw * 10 / 2560) !important;
      }

    }

    .login-btn {
      margin: calc(100vw * 40 / 2560) auto;
      height: calc(100vw * 100 / 2560);
      width: calc(100vw * 600 / 2560);
      font-size: calc(100vw * 36 / 2560);
      background: linear-gradient(180deg, #3670FF 0%, #1022D5 100%);
      box-shadow: inset 0px 0px 9px 1px #1C0007;
      border-radius: calc(100vw * 16 / 2560);
    }

    .reg {
      margin-top: 15px;
    }

    .xieyi {
      color: #333;
      margin-top: 20px;
    }

    .login-code {
      height: 38px;
      float: right;

      .login-code-img {
        height: 38px;
        position: absolute;
        margin-left: 10px;
        width: 200rpx;
      }
    }
  }
}
</style>
